package cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyoutcome;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyoutcome.vo.MoneyOutcomePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyoutcome.vo.MoneyOutcomeRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyoutcome.vo.MoneyOutcomeSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneyoutcome.MoneyOutcomeDO;
import cn.iocoder.yudao.module.erp.service.business.money.moneyoutcome.MoneyOutcomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 支出类型")
@RestController
@RequestMapping("/erp/money-outcome")
@Validated
public class MoneyOutcomeController {

    @Resource
    private MoneyOutcomeService moneyOutcomeService;

    @PostMapping("/create")
    @Operation(summary = "创建支出类型")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:create')")
    public CommonResult<Long> createMoneyOutcome(@Valid @RequestBody MoneyOutcomeSaveReqVO createReqVO) {
        return success(moneyOutcomeService.createMoneyOutcome(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支出类型")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:update')")
    public CommonResult<Boolean> updateMoneyOutcome(@Valid @RequestBody MoneyOutcomeSaveReqVO updateReqVO) {
        moneyOutcomeService.updateMoneyOutcome(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支出类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:delete')")
    public CommonResult<Boolean> deleteMoneyOutcome(@RequestParam("id") Long id) {
        moneyOutcomeService.deleteMoneyOutcome(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支出类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:query')")
    public CommonResult<MoneyOutcomeRespVO> getMoneyOutcome(@RequestParam("id") Long id) {
        MoneyOutcomeDO moneyOutcome = moneyOutcomeService.getMoneyOutcome(id);
        return success(BeanUtils.toBean(moneyOutcome, MoneyOutcomeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支出类型分页")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:query')")
    public CommonResult<PageResult<MoneyOutcomeRespVO>> getMoneyOutcomePage(@Valid MoneyOutcomePageReqVO pageReqVO) {
        PageResult<MoneyOutcomeDO> pageResult = moneyOutcomeService.getMoneyOutcomePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyOutcomeRespVO.class));
    }
    @GetMapping("/simple-list")
    @Operation(summary = "获得支出类型精简信息列表")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:query')")
    public CommonResult<List<MoneyOutcomeRespVO>> getMoneyOutcomeList() {
        MoneyOutcomePageReqVO moneyOutcomePageReqVO = new MoneyOutcomePageReqVO();
        moneyOutcomePageReqVO.setPageNo(PageParam.PAGE_SIZE_NONE);
        PageResult<MoneyOutcomeDO> moneyOutcomePage = moneyOutcomeService.getMoneyOutcomePage(moneyOutcomePageReqVO);
        return success(convertList(moneyOutcomePage.getList(),item -> new MoneyOutcomeRespVO().
                setId(item.getId()).setName(item.getName())));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出支出类型 Excel")
    @PreAuthorize("@ss.hasPermission('erp:money-outcome:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoneyOutcomeExcel(@Valid MoneyOutcomePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyOutcomeDO> list = moneyOutcomeService.getMoneyOutcomePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "支出类型.xls", "数据", MoneyOutcomeRespVO.class,
                        BeanUtils.toBean(list, MoneyOutcomeRespVO.class));
    }

}